import java.lang.ref.WeakReference;

/**
 * 当对象放在weakreference后不再被引用到
 * 则gc后对象被回收
 */
public class TestWeakReference {


    public static void main(String[] args) throws InterruptedException {

        Car car = new Car(22000, "silver");
        WeakReference<Car> weakCar = new WeakReference<Car>(car);

        int i = 0;

        long start = System.currentTimeMillis();
        while (true) {
            if (weakCar.get() != null) {
                i++;
                System.out.println("Object is alive for " + i + " loops - " + weakCar.get());
            } else {
                System.out.println("Object has been collected.");
//                System.out.println(car);
                break;
            }
        }
        long end = System.currentTimeMillis();

        System.out.println(end-start);
    }

}
